package week1;

/**
 * Created by dyc on 2016/10/14.
 */
public class QuickUnion extends UF {
    public QuickUnion(int n) {
        super(n);
    }

    private int root(int p){
        while (id[p] != p)
            p = id[p];
        return p;
    }
    @Override
    public void union(int p, int q) {
        super.union(p, q);
        id[p] = root(q);
    }

    @Override
    public boolean connected(int p, int q) {
        return root(p) == root(q);
    }

    @Override
    public int find(int p) {
        return root(p);
    }
}
